package org.dromara.hrp.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.hrp.domain.HrpLead;
import org.dromara.hrp.mapper.HrpLeadMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 线索商机Service业务层处理
 *
 * @author wenrui
 * @date 2024-03-21
 */
@RequiredArgsConstructor
@Service
public class HrpLeadServiceImpl extends BaseService {

    private final HrpLeadMapper baseMapper;

    /**
     * 查询线索商机
     */
    public HrpLead queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询线索商机列表
     */
    public TableDataInfo<HrpLead> queryPageList(HrpLead bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HrpLead> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(HrpLead::getNextTime);
        Page<HrpLead> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询线索商机列表
     */
    public List<HrpLead> queryList(HrpLead bo) {
        LambdaQueryWrapper<HrpLead> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<HrpLead> buildQueryWrapper(HrpLead bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HrpLead> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HrpLead::getName, bo.getName());
        lqw.eq(bo.getPartnerId() != null, HrpLead::getPartnerId, bo.getPartnerId());
        lqw.eq(bo.getSalesId() != null, HrpLead::getSalesId, bo.getSalesId());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), HrpLead::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getStage()), HrpLead::getStage, bo.getStage());
        lqw.eq(StringUtils.isNotBlank(bo.getLostReason()), HrpLead::getLostReason, bo.getLostReason());
        lqw.between(params.get("beginNextTime") != null && params.get("endNextTime") != null,
            HrpLead::getNextTime ,params.get("beginNextTime"), params.get("endNextTime"));
        lqw.orderByDesc(params.get("endNextTime") != null,HrpLead::getNextTime);
        return lqw;
    }

    /**
     * 新增线索商机
     */
    public Boolean insertByBo(HrpLead bo) {
        validEntityBeforeSave(bo);
        boolean flag = baseMapper.insert(bo) > 0;
        return flag;
    }

    /**
     * 修改线索商机
     */
    public Boolean updateByBo(HrpLead bo) {
        validEntityBeforeSave(bo);
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HrpLead entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除线索商机
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
